//
//  ORMyViewController.m
//  News
//
//  Created by rimi on 2016/12/8.
//  Copyright © 2016年 dongfumei. All rights reserved.
//

#import "ORMyViewController.h"

#import "ORMyTableViewCell.h"
#import "ORAboutViewController.h"
#import "ORInformationViewController.h"
#import "ORLoginsViewController.h"
#import "ORMYCollectionViewCell.h"

static NSString *const kCollectionViewReuseIndentifier=@"kCollectionViewReuseIndentifier";

@interface ORMyViewController () <UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
//背景
@property (nonatomic, strong) UIImageView *backImage;
//头像
@property (nonatomic, strong) UIImageView *headImage;
//姓名
@property (nonatomic, strong) UILabel *nameLabel;
//
@property (nonatomic, strong) UIVisualEffectView *effectView;
//退出按钮
@property (nonatomic, strong) UIButton *exitButton;

@property (nonatomic, strong)UIView    *bgbrowseView;
@property (nonatomic, strong)UICollectionView  *browesCollectionView;

@property (nonatomic, strong) NSMutableArray <DataSourceModel *> *opageArray;
@property (nonatomic, strong) UIButton *clicde;
@property (nonatomic, strong) NSMutableArray  <NSIndexPath *>*indexPaths;

@end

@implementation ORMyViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.opageArray = [Clickbrowse sharedClickbrowseDataModel].clickbrowseS;
//    NSLog(@"%@",self.opageArray);
    
    [self.browesCollectionView reloadData];
    
    if ([ORUserSingleton sharedUserInfo].isLogin) {
        [self assignment];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我";
    [self initUI];
}

- (void)initUI{
    self.indexPaths = [NSMutableArray array];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.backImage addSubview:self.nameLabel];
    [self.view addSubview:self.backImage];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headImage];
    [self.view addSubview:self.exitButton];
    [self.view addSubview:self.browesCollectionView];

    self.exitButton.hidden = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(LoginController)];
    [self.nameLabel addGestureRecognizer:tap];
    
    UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImageTap)];
    [self.headImage addGestureRecognizer:headTap];
}

- (void)assignment {
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:[ORUserSingleton sharedUserInfo].headImageUrl]];
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:[ORUserSingleton sharedUserInfo].headImageUrl]];
    self.nameLabel.text = [ORUserSingleton sharedUserInfo].accountName;
    self.exitButton.hidden = NO;
    [self.backImage addSubview:self.effectView];
}

#pragma mark - UITapGestureRecognizer

- (void)LoginController{
    ORLoginsViewController *login = [[ORLoginsViewController alloc]init];
    [self presentViewController:login animated:YES completion:nil];
}

- (void)headImageTap{
    
    ORInformationViewController *Information = [[ORInformationViewController alloc]init];
    
    if ([ORUserSingleton sharedUserInfo].isLogin) {
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:[ORUserSingleton sharedUserInfo].headImageUrl] ];
        [self.backImage sd_setImageWithURL:[NSURL URLWithString:[ORUserSingleton sharedUserInfo].headImageUrl] ];
        self.nameLabel.text = [ORUserSingleton sharedUserInfo].accountName;
    }else{
        
    }
    [self.navigationController pushViewController:Information animated:YES];
    
}

#pragma mark - ButtonEvent

- (void)exitButtonEvent{
    
    [[ORUserSingleton sharedUserInfo] loginOut];
    [self.effectView removeFromSuperview];
    self.headImage.image = [UIImage imageNamed:@""];
    self.backImage.image = [UIImage imageNamed:@"NEWS点"];
    self.exitButton.hidden = YES;
    self.nameLabel.text = @"请登录 先生/女士";
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuseIdentifier = @"ORMyTableViewCell";
    ORMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[ORMyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    if (indexPath.row == 2) {
        
        cell.iconView.image = [UIImage imageNamed:@"收藏箱"];
        cell.text_Label.text = @"浏览记录";
    }else if (indexPath.row == 0){
        cell.iconView.image = [UIImage imageNamed:@"个人中心"];
        cell.text_Label.text = @"个人信息";
    }else if (indexPath.row == 1){
        cell.iconView.image = [UIImage imageNamed:@"关于我们"];
        cell.text_Label.text = @"关于我们";
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        
    }else if (indexPath.row == 0){
        
        ORInformationViewController *Set = [[ORInformationViewController alloc]init];
        [self.navigationController pushViewController:Set animated:YES];
        
    }else if (indexPath.row == 1){
        
        ORAboutViewController *Information = [[ORAboutViewController alloc]init];
        [self.navigationController pushViewController:Information animated:YES];
    }
}

#pragma mark - *** UICollectionViewDataSource,UICollectionViewDelegate ***

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.opageArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ORMYCollectionViewCell *cell=(ORMYCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuseIndentifier  forIndexPath:indexPath];
    
    DataSourceModel *model =self.opageArray[indexPath.item];
    cell.srcName.text =model.src;
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil options:SDWebImageProgressiveDownload];
    cell.backgroundColor = model.isdeleted ? [UIColor colorWithRed:117/255.f green:123/255.f blue:159/255.f alpha:0.7] : [UIColor clearColor];
    if (self.opageArray.count >0) {
        [self.view addSubview:self.clicde];
    }
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.opageArray[indexPath.item].isdeleted = !self.opageArray[indexPath.item].isdeleted;
    UICollectionViewCell  *cell = [collectionView cellForItemAtIndexPath:indexPath];
    if (!self.opageArray[indexPath.item].isdeleted) {
        
        [self.indexPaths removeObject:indexPath];
        
        cell.backgroundColor =[UIColor clearColor];
        return;
    }
    cell.backgroundColor=[UIColor colorWithRed:117/255.f green:123/255.f blue:159/255.f alpha:0.7];
    
    [self.indexPaths addObject:indexPath];
}

#pragma mark -

-(void)clickDeleted:(UIButton *)sender{
    
    if (self.indexPaths.count == 0 && self.opageArray.count == 0) {
        
        return;
    }
    
    NSMutableIndexSet *indexSets = [NSMutableIndexSet indexSet];
    [self.indexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [indexSets addIndex:obj.item];
    }];
    [self.opageArray removeObjectsAtIndexes:indexSets];
    if (self.opageArray.count == 0) {
        [self.clicde removeFromSuperview];
    }
    
    [self.browesCollectionView deleteItemsAtIndexPaths:self.indexPaths];
    
    [[Clickbrowse sharedClickbrowseDataModel] save];
    
    [self.indexPaths removeAllObjects];
    
}

#pragma mark - getter

- (UIVisualEffectView *)effectView {
    
    if (!_effectView) {
        
        //模糊效果
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        _effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
        _effectView.frame = self.backImage.bounds;
        _effectView.alpha = 0.5;
        
    }
    return _effectView;
}

- (UIImageView *)backImage{
    if (!_backImage) {
        _backImage = [[UIImageView alloc]initWithFrame:AAdaptionRect(0, 0, kBaseWidth, 250)];
        _backImage.image = [UIImage imageNamed:@"NEWS点"];
        _backImage.userInteractionEnabled = YES;
    }
    return _backImage;
}

- (UIImageView *)headImage{
    if (!_headImage) {
        _headImage = [[UIImageView alloc]initWithFrame:AAdaptionRect(kBaseWidth/2-45, 85, 90, 90)];
        _headImage.layer.borderWidth = 2;
        _headImage.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        _headImage.layer.cornerRadius = AAdaptation(45);
        _headImage.layer.masksToBounds = YES;
        //开启交互
        _headImage.userInteractionEnabled = YES;
    }
    return _headImage;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:AAdaptionRect(kBaseWidth/2-100, CGRectGetMaxY(self.headImage.frame)/AAdaptationWidth()+20, 200, 30)];
        _nameLabel.font = AAFont(18);
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.text = @"请登录 先生/女士";
        _nameLabel.font = [UIFont boldSystemFontOfSize:18];
        _nameLabel.textColor = [UIColor whiteColor];
        //开启交互
        _nameLabel.userInteractionEnabled = YES;
    }
    return _nameLabel;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:AAdaptionRect(0, CGRectGetMaxY(self.backImage.frame)/AAdaptationWidth(), kBaseWidth/AAdaptationWidth(), 132/AAdaptationWidth()) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

- (UIView *)bgbrowseView{
    
    if (!_bgbrowseView) {
        _bgbrowseView=[[UIView alloc]initWithFrame:AAdaptionRect(0, CGRectGetMaxY(self.tableView.frame)/AAdaptationWidth(), kBaseWidth, 110)];
        _bgbrowseView.backgroundColor =[UIColor grayColor];
    }
    
    return _bgbrowseView;
}

- (UICollectionView *)browesCollectionView{
    
    if (!_browesCollectionView) {
        
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = AAdaptationSize(105, 65);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 10;
        [layout setSectionInset:UIEdgeInsetsMake(0, 10, 10, 0)];
        
        _browesCollectionView = [[UICollectionView alloc]initWithFrame:AAdaptionRect(0, CGRectGetMaxY(self.tableView.frame)/AAdaptationWidth(), kBaseWidth, 110) collectionViewLayout:layout];
        _browesCollectionView.delegate=self;
        _browesCollectionView.dataSource=self;
        _browesCollectionView.backgroundColor=[UIColor groupTableViewBackgroundColor];
        
        //注重重用
        [_browesCollectionView registerClass:[ORMYCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuseIndentifier];
        }
    return _browesCollectionView;
}


- (UIButton *)clicde{
    
    if (!_clicde) {
        
        _clicde = [[UIButton alloc]initWithFrame:AAdaptionRect(kBaseWidth - 80, CGRectGetMaxY(self.tableView.frame)/AAdaptationWidth() - 47, 60, 44)];
        [_clicde setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
        [_clicde setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_clicde setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
        [_clicde addTarget:self action:@selector(clickDeleted:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _clicde;
}

- (UIButton *)exitButton{
    if (!_exitButton) {
        _exitButton = [[UIButton alloc]initWithFrame:AAdaptionRect(0, 0, 280, 50)];
        _exitButton.center = AAadaptationPoint(kBaseWidth/2, CGRectGetMaxY(self.browesCollectionView.frame)/AAdaptationWidth()+30);
        [_exitButton setTitle:@"退   出" forState:UIControlStateNormal];
        _exitButton.backgroundColor = MainColor;
        _exitButton.layer.cornerRadius = 3;
        [_exitButton addTarget:self action:@selector(exitButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _exitButton;
}


@end
